package com.scsj.supermarket.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scsj.supermarket.R;
import com.scsj.supermarket.bean.ModelDishBean;
import com.scsj.supermarket.bean.ModelDishMenuBean;
import com.scsj.supermarket.bean.ModelShopCartBean;
import com.scsj.supermarket.interfaces.ShopCartInterface;
import com.scsj.supermarket.utils.Logger;

import java.util.ArrayList;

import es.dmoral.toasty.Toasty;

/**
 * 商家店铺右边商品页面
 */

public class RightDishAdapter extends RecyclerView.Adapter {

    private final int MENU_TYPE = 0;
    private final int DISH_TYPE = 1;
    private final int HEAD_TYPE = 2;
    private Context mContext;
    private ArrayList<ModelDishMenuBean> mMenuList;
    private int mItemCount;
    private ModelShopCartBean mModelShopCart;
    private ShopCartInterface shopCartImp;

    public RightDishAdapter(Context mContext, ArrayList<ModelDishMenuBean> mMenuList, ModelShopCartBean modelShopCart) {
        this.mContext = mContext;
        this.mMenuList = mMenuList;
        this.mItemCount = mMenuList.size();
        this.mModelShopCart = modelShopCart;
        for (ModelDishMenuBean menu : mMenuList) {
            mItemCount += menu.getModelDishList().size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        int sum = 0;
        for (ModelDishMenuBean menu : mMenuList) {
            if (position == sum) {
                return MENU_TYPE;
            }
            sum += menu.getModelDishList().size() + 1;
        }
        return DISH_TYPE;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == MENU_TYPE) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.right_menu_item_top, parent, false);
            MenuViewHolder viewHolder = new MenuViewHolder(view);
            return viewHolder;
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.right_dish_item_bottom, parent, false);
            DishViewHolder viewHolder = new DishViewHolder(view);
            return viewHolder;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (getItemViewType(position) == MENU_TYPE) {
            MenuViewHolder menuholder = (MenuViewHolder) holder;

            if (menuholder != null) {
                menuholder.right_menu_title.setText(getMenuByPosition(position).getMenuName());
                menuholder.right_menu_layout.setContentDescription(position + "");
            }

        } else {

            final DishViewHolder dishholder = (DishViewHolder) holder;
            if (dishholder != null) {

                final ModelDishBean modelDish = getDishByPosition(position);
                dishholder.right_dish_name_tv.setText(modelDish.getDishName());

                dishholder.right_dish_price_tv.setText("¥" + modelDish.getDishPrice());
                dishholder.right_dish_layout.setContentDescription(position + "");
                dishholder.market_oldprice_tv.setText("¥" + modelDish.getDishAmount());

                int count = 0;
                if (mModelShopCart.getShoppingSingleMap().containsKey(modelDish)) {
                    count = mModelShopCart.getShoppingSingleMap().get(modelDish);
                }
                if (count <= 0) {

                    dishholder.right_dish_add_iv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (mModelShopCart.addShoppingSingle(modelDish)) {
                                notifyItemChanged(position);
                                if (shopCartImp != null)
                                    shopCartImp.add(view, position);
                            }
                        }
                    });

                }
            }
        }
    }

    public void setData(ArrayList<ModelDishMenuBean> mMenuList){
        this.mMenuList = mMenuList;
        this.notifyDataSetChanged();
    }

    public ModelDishMenuBean getMenuByPosition(int position) {
        int sum = 0;
        for (ModelDishMenuBean menu : mMenuList) {
            if (position == sum) {
                return menu;
            }
            sum += menu.getModelDishList().size() + 1;
        }
        return null;
    }

    public ModelDishBean getDishByPosition(int position) {
        for (ModelDishMenuBean menu : mMenuList) {
            if (position > 0 && position <= menu.getModelDishList().size()) {
                return menu.getModelDishList().get(position - 1);
            } else {
                position -= menu.getModelDishList().size() + 1;
            }
        }
        return null;
    }

    public ModelDishMenuBean getMenuOfMenuByPosition(int position) {
        for (ModelDishMenuBean menu : mMenuList) {
            if (position == 0) return menu;
            if (position > 0 && position <= menu.getModelDishList().size()) {
                return menu;
            } else {
                position -= menu.getModelDishList().size() + 1;
            }
        }
        return null;
    }

    @Override
    public int getItemCount() {
        return mItemCount;
    }

    public ShopCartInterface getShopCartInterface() {
        return shopCartImp;
    }

    public void setShopCartInterface(ShopCartInterface shopCartImp) {
        this.shopCartImp = shopCartImp;
    }

    private class MenuViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout right_menu_layout;
        private TextView right_menu_title;

        public MenuViewHolder(View itemView) {
            super(itemView);
            right_menu_layout = itemView.findViewById(R.id.right_menu_ll);
            right_menu_title =  itemView.findViewById(R.id.right_menu_tv);
        }
    }

    private class DishViewHolder extends RecyclerView.ViewHolder {

        private TextView right_dish_name_tv;
        private TextView right_dish_price_tv;
        private LinearLayout right_dish_layout;
        private ImageView right_dish_add_iv;
        private TextView market_oldprice_tv;

        public DishViewHolder(View itemView) {
            super(itemView);
            right_dish_name_tv = itemView.findViewById(R.id.right_dish_name);
            right_dish_price_tv =  itemView.findViewById(R.id.right_dish_price);
            right_dish_layout = itemView.findViewById(R.id.right_dish_item);
            right_dish_add_iv =  itemView.findViewById(R.id.right_dish_add);
            market_oldprice_tv = itemView.findViewById(R.id.market_oldprice_tv);
        }
    }
}
